<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="cpName">
        <el-input
          v-model="queryParams.cpName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="principal">
        <el-input
          v-model="queryParams.principal"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:circuit:add']"
        >新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:circuit:edit']"
        >修改
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['production:circuit:remove']"
        >删除
        </el-button>
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['production:circuit:export']"-->
        <!--        >导出-->
        <!--        </el-button>-->
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="circuitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="产品工艺线路id" align="center" prop="circuitId" />-->
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="工序名称">
              <span v-for="(item,index) in props.row.flowList">
                {{ index + 1 }}
                {{ item.gxName }}
              </span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="cpName" width="200" show-overflow-tooltip/>
      <el-table-column label="负责人" align="center" prop="principal"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['production:circuit:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['production:circuit:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品工艺线路对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="cpName">
          <el-input v-model="form.cpName" placeholder="请输入产品名称"/>
        </el-form-item>
        <!--        <el-form-item label="负责人" prop="principal">-->
        <!--          <el-input v-model="form.principal" placeholder="请输入负责人" />-->
        <!--        </el-form-item>-->
        <el-divider content-position="center">产品工艺流程信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFlow">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFlow">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="flowList" :row-class-name="rowFlowIndex" @selection-change="handleFlowSelectionChange"
                  ref="flow">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="工序名称" prop="gxName" width="150">
            <template slot-scope="scope">
              <!--              <el-input v-model="scope.row.gxName" placeholder="请输入工序名称"/>-->
              <el-select v-model="scope.row.gxName" placeholder="请选择工序名称">
                <el-option :key="item.stepsId" v-for="item in stepsList" :label="item.gxName"
                           :value="item.gxName"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="向上">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="upBtn(scope.row)"
              >向上
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="向下">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="dowmBtn(scope.row)"
              >向下
              </el-button>
            </template>
          </el-table-column>
          <!--          <el-table-column label="工序设置表外键FK" prop="stepsId" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.stepsId" placeholder="请输入工序设置表外键FK"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->

          <!--          <el-table-column label="排序" prop="sort" width="150">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.sort" placeholder="请输入排序" />-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCircuit, getCircuit, delCircuit, addCircuit, updateCircuit} from "@/api/production/circuit";
import {listSteps} from "@/api/production/steps";
import {listManagement} from "@/api/production/management";

export default {
  name: "Circuit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFlow: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品工艺线路表格数据
      circuitList: [],
      // 产品工艺流程表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        cpName: null,
        principal: null,
      },
      // 表单参数
      form: {},
      managementList: [],
      // 表单校验
      rules: {
        cpName: [
          {required: true, trigger: "blur", message: "请输入产品名称"}
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      getgongxuList: null
    };
  },
  created() {
    this.getList();
    // this.getgongxuList()
    this.getLista()
    // console.log(this.$store.state.user.name)
  },
  watch: {
    flowList: {
      handler(newValue, oldValue) {
        console.log('new', newValue)
        console.log('old', oldValue)
      }
    }
  },
  methods: {

    // 获取产品列表
    getchanpinList() {
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
      });
    },
    getLista() {
      listSteps(this.queryParams).then(response => {
        this.stepsList = response.rows;
      });
    },
    /** 查询产品工艺线路列表 */
    getList() {
      this.loading = true;
      listCircuit(this.queryParams).then(response => {
        this.circuitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        circuitId: null,
        cpName: null,
        principal: null,
        createTime: null
      };
      this.flowList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.circuitId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品工艺线路";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const circuitId = row.circuitId || this.ids
      getCircuit(circuitId).then(response => {
        this.form = response.data;
        this.flowList = response.data.flowList;
        this.open = true;
        this.title = "修改产品工艺线路";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.flowList = this.flowList;
          if (this.form.circuitId != null) {
            updateCircuit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.principal = this.$store.state.user.name;
            addCircuit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const circuitIds = row.circuitId || this.ids;
      this.$modal.confirm('是否确认删除产品工艺线路编号为"' + circuitIds + '"的数据项？').then(function () {
        return delCircuit(circuitIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 产品工艺流程序号 */
    rowFlowIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 产品工艺流程添加按钮操作 */
    handleAddFlow() {
      let obj = {};
      obj.stepsId = "";
      obj.gxName = "";
      obj.sort = "";
      this.flowList.push(obj);
    },

    upBtn(row) {
      let tempOption = this.flowList[row.index - 1]
      console.log(tempOption)
      this.$set(this.flowList, row.index - 1, this.flowList[row.index])
      this.$set(this.flowList, row.index, tempOption)
    },
    dowmBtn(row) {
      row.index = row.index + 1
    },
    /** 产品工艺流程删除按钮操作 */
    handleDeleteFlow() {
      if (this.checkedFlow.length == 0) {
        this.$modal.msgError("请先选择要删除的产品工艺流程数据");
      } else {
        const flowList = this.flowList;
        const checkedFlow = this.checkedFlow;
        this.flowList = flowList.filter(function (item) {
          return checkedFlow.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFlowSelectionChange(selection) {
      this.checkedFlow = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('production/circuit/export', {
        ...this.queryParams
      }, `circuit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
